package com.makefuture.config;


import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.Const;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.tx.TxByActionKeyRegex;
import com.jfinal.plugin.activerecord.tx.TxByActionKeys;
import com.jfinal.plugin.activerecord.tx.TxByMethodRegex;
import com.jfinal.plugin.activerecord.tx.TxByMethods;
import com.jfinal.plugin.scheduler.SchedulerPlugin;
import com.jfinal.render.ViewType;
import com.makefuture.controller.DataInterface;
import com.makefuture.controller.ExportController;
import com.makefuture.controller.ProlemController;
import com.makefuture.controller.UploadController;
import com.makefuture.controller.UserController;
import com.makefuture.controller.indexController;
import com.makefuture.task.OneDayJob;
import com.makefuture.utils.DateUtil;

public class InitConfig extends JFinalConfig{
	
	public final static String relativePath="images/"+DateUtil.getCurrentDate();
	public final static String filePath=PathKit.getWebRootPath()+"//"+relativePath;

	/**
	 * 配置常量
	 */
	@Override
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值 
		try {//如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
			PropKit.use("javen_config_pro.txt");//生产环境配置文件
		}
		catch (Exception e) {
			PropKit.use("a_little_config.txt");//开发环境配置文件
		}
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setEncoding("utf-8");
		me.setViewType(ViewType.JSP);
		//me.setError404View(error404View);
	//	me.setBaseUploadPath(filePath);//文件上传保存路径
		me.setMaxPostSize(1*Const.DEFAULT_MAX_POST_SIZE);//上传文件最大为10M
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/",indexController.class);
		me.add("/user",UserController.class,"/pages/user");
		me.add("/problerm",ProlemController.class,"/pages/problem");
		me.add("/exports", ExportController.class,"/pages/export");
		me.add("/upload",UploadController.class,"/pages/upload");
		me.add("/data",DataInterface.class);
		
	}

	@Override
	public void configPlugin(Plugins me) {
//		//c3p0连接池
//		 C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl").trim(), PropKit.get("user").trim(), PropKit.get("password").trim(),PropKit.get("jdbcDriver").trim());
//		 me.add(c3p0Plugin);
//		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
//	 	_MappingKit.mapping(arp);
//		// 配置大小写不敏感
//		arp.setContainerFactory(new CaseInsensitiveContainerFactory(true)); 
//		arp.setShowSql(true);
//		me.add(arp);
//	    arp.setDialect(new SqlServerDialect()); //此处是重点，不然会出错
//	    
//	    QuartzPlugin quartzPlugin =  new QuartzPlugin("job.properties");
//        me.add(quartzPlugin);
		
//		 SchedulerPlugin sp = new SchedulerPlugin();
//	        Runnable task = new OneDayJob();
//	        //每隔10秒执行一次
//	        //sp.fixedDelaySchedule(task, 10);
//	        //sp.fixedRateSchedule(task, 10);
//	        //每隔1分钟执行一次
//	        sp.cronSchedule(task, "* * * * *");
//	        me.add(sp);
        
//        QuartzPlugin quartzPlugin = new QuartzPlugin("quartzjob.properties","quartz.properties");
//        quartzPlugin.add("* * * * * *",new JobB());
       // quartzPlugin.version(QuartzPlugin.VERSION_1);
//	  //初始化一个任务调度插件,参数为配置文件名
//	    SchedulerPlugin sp = new SchedulerPlugin("job.properties");
//	   // 添加插件
//	    sp.cronSchedule(task, "* * * * *");
//	    me.add(sp);
		 SchedulerPlugin sp = new SchedulerPlugin("job.properties");
	       me.add(sp);
	    
//	    me.add(new Cron4jPlugin());

	}

	@Override
	public void configInterceptor(Interceptors me) {
		//arg0.add(new GlobalInterceptor());//全局拦截器 拦截所有url请求
		me.add(new TxByMethodRegex("(.*save.*|.*update.*)"));//类的方法名称校验
		me.add(new TxByMethods("save", "update"));
		me.add(new TxByActionKeyRegex("/*.*"));//类名校验
		me.add(new TxByActionKeys("/tx/save", "/tx/update"));
	}

	@Override
	public void configHandler(Handlers me) {
		//设置上下文路径
		me.add(new ContextPathHandler("contextPath"));
		// ${contextPath} 即可获取  上下文即项目名称
	}

}
